<template>
  <div 
    class="comment-popup"
    :style="{
      top: position.top + 'px',
      left: position.left + 'px',
      width: width + 'px',
      display: visible ? 'flex' : 'none'
    }"
    v-click-outside="handleClickOutside"
  >
    <div class="popup-content">
      <div class="popup-header">
        <h3 style="color: #333333;">评论</h3>
        <button class="close-btn" @click="closePopup">×</button>
      </div>
      
      <div class="selected-text" v-if="selectedText">
        <div class="popup-header">选中内容：</div>
        <p>"{{ selectedText }}"</p>
      </div>
      
      <textarea
        v-model="commentContent"
        class="comment-input"
        placeholder="写下你的评论..."
        rows="5"
        ref="commentInput"
      ></textarea>
      
      <div class="popup-actions">
        <button
          @click="submitComment"
          class="submit-btn"
          :disabled="!commentContent.trim()"
        >
          提交评论
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref} from 'vue'
import {createCommentAPI} from "@/api/comment.ts"
import {getUserId} from "@/utils/auth.ts";
import type {Comment} from "@/types/index.ts"
import Editor from './Editor.vue';


const commentContent = ref('')
const commentInput = ref<HTMLTextAreaElement | null>(null)

const error = ref<string | null>(null)
const props = defineProps({
  selectedText: {
    type: String,
    required: true
  },
  position: {
    type: Object,
    default: () => ({ top: 0, left: 0 })
  },
  width: {
    type: Number,
    default: 320
  },
  visible: {
    type: Boolean,
    default: true
  },
  documentId: {
    type: String,
    required:true
  }
})

const emit = defineEmits(['close','comment-created'])
// 点击外部关闭弹窗
const handleClickOutside = () => {
  closePopup()
}

// 关闭弹窗
const closePopup = () => {
  emit('close')
}



// 提交评论
const submitComment = async () => {
  if (!commentContent.value.trim()) return

  try {
    await createCommentAPI({
      documentId:props.documentId,
      content: commentContent.value,
      creatorId: getUserId()?.toString() || '', 
      timestamp: new Date(),
      selectedText: props.selectedText
    })

    commentContent.value = ''
    emit('close')
    emit('comment-created')
    
  } catch (err) {
    console.error('创建评论失败:', err)
    throw err
  }
}

</script>
<style scoped>
.comment {
  background-color: red;
  cursor: pointer;
}
.comment-popup {
  position: fixed;
  z-index: 1001;
  background-color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: 80vh;
  animation: fadeIn 0.2s ease-out;
}

.popup-content {
  display: flex;
  flex-direction: column;
  padding: 16px;
  height: 100%;
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  color: #333;
}

.popup-header h3 {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #666;
  padding: 0 8px;
}

.close-btn:hover {
  color: #333;
}

.selected-text {
  background-color: #f5f7fa;
  padding: 8px 12px;
  border-radius: 4px;
  margin-bottom: 12px;
  font-size: 14px;
}

.selected-text p {
  margin: 4px 0 0;
  color: #666;
  white-space: pre-wrap;
  word-break: break-word;
}

.comment-input {
  width: 90%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  resize: none;
  margin-bottom: 12px;
  font-size: 14px;
  min-height: 120px;
  flex: 1;
}

.comment-input:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.popup-actions {
  display: flex;
  justify-content: flex-end;
}

.submit-btn {
  background-color: #1890ff;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s;
}

.submit-btn:hover {
  background-color: #40a9ff;
}

.submit-btn:disabled {
  background-color: #d9d9d9;
  cursor: not-allowed;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>